package com.adb.receivecontent.ui;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.Toast;

import com.adb.receivecontent.R;
import com.adb.receivecontent.bean.LinkMagnetBean;
import com.adb.receivecontent.bean.LinkParam;
import com.adb.receivecontent.uc.AddParamsAdapter;
import com.adb.receivecontent.uc.IMenuClickListener;
import com.adb.receivecontent.uc.LinkMagnetAdapter;
import com.adb.receivecontent.utility.Const;

public class NewLinkCreateActivity extends Activity implements
		IMenuClickListener, OnClickListener, OnFocusChangeListener {

	private final String TAG = ReceiveContentActivity.class.getCanonicalName();
	private ListView listview_created_linked;
	private AddParamsAdapter mAddParamAdapter;

	private EditText medit_text_link_name, medit_text_url_name,medit_text_file_name;

	private Button btn_new_link_creating, btn_link_cancel;

	private Button btn_additional_var;

	private Dialog mAddtionalVarDialog;
	
	private String strModeActive;
	
	private int mPosition;

	private LinkMagnetBean mEditLinkMagment; 
	
	private ScrollView scroll_view_group;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG, "[ onCreate ] ");
		setContentView(R.layout.activity_create_new_link);
		
		if(getIntent().getExtras() != null){
			
			if(getIntent().getExtras().getString(Const.INTENT_KEY_MODE).equals(Const.INTENT_MODE_VALUE_EDIT)){
				
				strModeActive = Const.INTENT_MODE_VALUE_EDIT;
				
				mPosition = Integer.parseInt(getIntent().getExtras().getString(Const.INTENT_KEY_LOCATION));
				
				mEditLinkMagment = LinkMagnetAdapter.mLinkListLinkMagnet.get(mPosition);
				
				
			}else if(getIntent().getExtras().getString(Const.INTENT_KEY_MODE).equals(Const.INTENT_MODE_VALUE_NEW)){
				strModeActive = Const.INTENT_MODE_VALUE_NEW;
			}
		}
		
		scroll_view_group = (ScrollView)findViewById(R.id.scroll_view_group);
		
		medit_text_link_name = (EditText) findViewById(R.id.edit_text_link_name);
		medit_text_url_name = (EditText) findViewById(R.id.edit_text_url_name);
		medit_text_file_name = (EditText) findViewById(R.id.edit_text_file_name);

		if(strModeActive.equals(Const.INTENT_MODE_VALUE_EDIT)){
			
			medit_text_link_name.setText(mEditLinkMagment.getStrlinkName());
			medit_text_url_name.setText(mEditLinkMagment.getStrlinkurl());
			medit_text_file_name.setText(mEditLinkMagment.getStrFileName());
		}
		
		listview_created_linked = (ListView) findViewById(R.id.listview_created_linked);
		mAddParamAdapter = new AddParamsAdapter(NewLinkCreateActivity.this);
		listview_created_linked.setAdapter(mAddParamAdapter);
		listview_created_linked.setEmptyView(findViewById(R.id.empty));
		
		if(strModeActive.equals(Const.INTENT_MODE_VALUE_EDIT)){
			
			if(mEditLinkMagment != null && mEditLinkMagment.getmArrayListParams() != null){
				
				mAddParamAdapter.addParamsLists(mEditLinkMagment.getmArrayListParams());	
			}
		}
		
		btn_new_link_creating = (Button) findViewById(R.id.btn_new_link_creating);
		btn_new_link_creating.setOnClickListener(this);
		btn_link_cancel = (Button) findViewById(R.id.btn_link_cancel);
		btn_link_cancel.setOnClickListener(this);

		btn_additional_var = (Button) findViewById(R.id.btn_additional_var);
		btn_additional_var.setOnClickListener(this);
	}
	
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		// Checks the orientation of the screen
	    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
	    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
	    }
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		Log.d(TAG, "[ onStart ] ");
	}

	@Override
	protected void onStop() {
		super.onStop();
		Log.d(TAG, "[ onStop ] ");
	}

	@Override
	protected void onPause() {
		super.onPause();
		Log.d(TAG, "[ onPause ] ");
	}

	@Override
	protected void onResume() {
		super.onResume();
		Log.d(TAG, "[ onResume ] ");
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.d(TAG, "[ onDestroy ] ");
	}

	@Override
	public void onLinkMenuClick(int id,Object location) {

		if (id == R.id.imageview_edit_params) {
			
			showAdditionalDialog(2,Integer.parseInt(location.toString()));
			
		} else if (id == R.id.imagview_delete_params) {
			
			mAddParamAdapter.removeItems(Integer.parseInt(location.toString()));
		}
	}

	@Override
	public void onClick(View v) {

		if (v.getId() == R.id.btn_new_link_creating) {

			if (medit_text_link_name.getText().toString().length() > 0
					|| !medit_text_link_name.getText().toString().equals("")) {

				if (medit_text_url_name.getText().toString().length() > 0
						|| !medit_text_url_name.getText().toString().equals("")) {

					if (medit_text_file_name.getText().toString().length() > 0
							|| !medit_text_file_name.getText().toString()
									.equals("")) {
						
						if(strModeActive.equals(Const.INTENT_MODE_VALUE_NEW)){
							
							LinkMagnetBean mLinkBean = new LinkMagnetBean();

							// link name
							mLinkBean.setStrlinkName(medit_text_link_name.getText()
									.toString().trim());
							// link url to post
							mLinkBean.setStrlinkurl(medit_text_url_name.getText()
									.toString().trim());
							// file name
							mLinkBean.setStrFileName(medit_text_file_name.getText()
									.toString().trim());

							// additional params
							mLinkBean.setmArrayListParams(mAddParamAdapter.getCurrentListofLinkParams());
									
							// commit inside preference 
							mLinkBean.pushNewLinkDataSet();
							
							Intent mIntentComplete = new Intent();
							
							mIntentComplete.putExtra(Const.INTENT_KEY_STATUS,"DONE");
							
							setResult(RESULT_OK, mIntentComplete);

							finish();
							

						}else if(strModeActive.equals(Const.INTENT_MODE_VALUE_EDIT)){
								
							LinkMagnetBean mLinkBean = new LinkMagnetBean();
							
							// link name
							mLinkBean.setStrlinkName(medit_text_link_name.getText().toString().trim());
									
							// link url to post
							mLinkBean.setStrlinkurl(medit_text_url_name.getText().toString().trim());
									
							// file name
							mLinkBean.setStrFileName(medit_text_file_name.getText().toString().trim());
									
							// additional params
							mLinkBean.setmArrayListParams(mAddParamAdapter.getCurrentListofLinkParams());
							
//							LinkMagnetAdapter.mLinkListLinkMagnet.remove(mPosition);
							
//							LinkMagnetAdapter.mLinkListLinkMagnet.add(mPosition,mLinkBean);
							
							mLinkBean.pushUpdateLinkDataSet(mEditLinkMagment.getStrLinkID());
							
							Intent mIntentComplete = new Intent();
							
							mIntentComplete.putExtra(Const.INTENT_KEY_STATUS,"DONE");
							
							setResult(RESULT_OK, mIntentComplete);

							finish();
							
						}
						
					} else {
						Toast.makeText(NewLinkCreateActivity.this,"You miss file name", Toast.LENGTH_LONG).show();
					}

				} else {
					
					Toast.makeText(NewLinkCreateActivity.this,"You miss Url name", Toast.LENGTH_LONG).show();
				}

			} else {
				
				Toast.makeText(NewLinkCreateActivity.this,"You miss link name", Toast.LENGTH_LONG).show();
			}

		} else if (v.getId() == R.id.btn_link_cancel) {

			Intent mIntentComplete = new Intent();
			setResult(RESULT_CANCELED, mIntentComplete);

			finish();

		} else if (v.getId() == R.id.btn_additional_var) {
			showAdditionalDialog(1,-1);
		}
	}

	/**
	 * Mode 1 is add new item 
	 * Mode 3 is edit item
	 * @param mode
	 * @param position
	 */
	public void showAdditionalDialog(final int mode,final int position) {

		mAddtionalVarDialog = new Dialog(NewLinkCreateActivity.this,R.style.MyDialogTheme);
		mAddtionalVarDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		mAddtionalVarDialog.setContentView(R.layout.custom_dialog_additional);

		final EditText mEdit_text_key = (EditText) mAddtionalVarDialog
				.findViewById(R.id.edit_text_key);
		mEdit_text_key.setSingleLine(true);
		mEdit_text_key.setOnFocusChangeListener(NewLinkCreateActivity.this);
		final EditText mEdit_text_value = (EditText) mAddtionalVarDialog
				.findViewById(R.id.edit_text_value);
		mEdit_text_value.setSingleLine(true);
		mEdit_text_value.setOnFocusChangeListener(NewLinkCreateActivity.this);

		Button mbtn_addtional_ok = (Button) mAddtionalVarDialog
				.findViewById(R.id.btn_addtional_ok);
		Button mbtn_addtional_cancel = (Button) mAddtionalVarDialog
				.findViewById(R.id.btn_addtional_cancel);

		// for add new items
		if (mode == 1) {

		} else if (mode == 2) {
			// for edit items
			LinkParam mTempParams = mAddParamAdapter.getLocationParams(position);
			if(mTempParams != null){
				
				mEdit_text_key.setText(mTempParams.getStrKey());
				mEdit_text_value.setText(mTempParams.getStrValue());	
			}
			
		}

		mbtn_addtional_cancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				
				  hideKeyBoard(mEdit_text_key);
				  mAddtionalVarDialog.dismiss();
			}
		});

		mbtn_addtional_ok.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				
				LinkParam mNewLinkParams = new LinkParam();
				mNewLinkParams.setStrKey(mEdit_text_key.getText().toString());
				mNewLinkParams.setStrValue(mEdit_text_value.getText().toString());
				
				if (mode == 1) {
					mAddParamAdapter.addParmasItems(mNewLinkParams);
				} else if (mode == 2) {
					mAddParamAdapter.replaceItems(position,mNewLinkParams);
				}
				
				hideKeyBoard(mEdit_text_key);
				mAddtionalVarDialog.dismiss();
			}
		});

		mAddtionalVarDialog.show();
	}

	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		View mView = v;

		if (mView instanceof EditText) {
			hideKeyBoard((EditText) mView);
		}
	}

	public void hideKeyBoard(EditText editTextTemp) {
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		if (editTextTemp != null) {
			imm.hideSoftInputFromWindow(editTextTemp.getWindowToken(), 0);
		}
	}

}
